package com.as.server;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import com.as.measurement.model.ClientInterface;
import com.as.measurement.model.UserInterfaces;
import com.as.measurement.server.service.ServerService;





public class GWTServiceComponent {
	
	public static GWTServiceComponent  s_instance;
	HashMap<String, ClientInterface> _ht;
	ServerService _qos=null;
	
	public GWTServiceComponent() {
	}
	
	public static GWTServiceComponent getInstance(){
		if (s_instance == null) {
			createInstance();
		}

		return s_instance;
	}
	
	private static void createInstance(){
		s_instance = new GWTServiceComponent();
	}
	
	public void setQoSService(ServerService qos ){
		_qos = qos;
		_ht=_qos.getQoSParams();
		
	}
	
	public String getHello(){
		return "Hello";
	}
	
//	private  HashMap<String,ClientInterface> getQoSParams (){
//		HashMap<String, ClientInterface> ht = _qos.getQoSParams();
//		if (ht==null){
//			return null; 
//		}
//		else {
//			return ht;
//		}
//	}
	
	public ArrayList<ArrayList<Double>> getJitter(){
		ArrayList<ArrayList<Double>> jitterList = new ArrayList<ArrayList<Double>>(); 
//		HashMap<String, ClientInterface> ht = _qos.getQoSParams();
		if (_ht==null){
			return null; 
		}
		else{
			Set<String> set = _ht.keySet();
			for (String ee: set){
				ClientInterface client = UserInterfaces.userInterfaces.get(ee);
				ArrayList  jitter = client.get_jitter();
				
				// print 
				int count = 0;
				System.out.println( "********************************GWT COMPO\n\n");
				for ( Object e: jitter ){
					System.out.println("id:"+client.get_id());
					System.out.println("jitter:"+jitter.get(count));
					count++;
				}
				System.out.println( "********************************GWT COMPO\n\n");
				
				jitterList.add(jitter);
				System.out.println( "GWT COMPO jitter añadido");
			}
			if(jitterList==null){
				System.out.println( "GWT COMPO lista vacia ");
				return null;
			}
			else {
                /*
                 * Cut and resize the arrayList. Display only the last 30 elements
                 */
                resizeTo30(jitterList);
				System.out.println("COMPO retorna la lista de jitter");
				return jitterList;
			}
		}
		
		
		
	}
	
	public ArrayList<ArrayList<Timestamp>> getTime(){
		ArrayList<ArrayList<Timestamp>> time= new ArrayList<ArrayList<Timestamp>>();
//		HashMap<String, ClientInterface> ht = _qos.getQoSParams();
		if (_ht==null){
			return null; 
		}
		else{
			Set<String> set = _ht.keySet();
			for (String ee: set){
				ClientInterface client = UserInterfaces.userInterfaces.get(ee);
				ArrayList<Timestamp> timeList = client.get_time();
				// print 
				int count = 0;
				System.out.println( "********************************GWT COMPO\n\n");
				for ( Object e: timeList ){
					System.out.println("time:"+timeList.get(count));
					count++;
				}
				System.out.println( "********************************GWT COMPO \n\n");
				time.add(timeList);
				System.out.println( "GWT COMPO time añadido");
			}
			if(time==null){
				System.out.println( "GWT COMPO lista time vacia ");
				return null;
			}
			else {
				
                /*
                 * Cut and resize the arrayList. Display only the last 30 elements
                 */
                resizeTo30(time);
                
				System.out.println("COMPO retorna la lista de tiempo ");
				return time;
			}	
		}
		
	}
	
	public ArrayList<ArrayList<Double>> getBW(){
		ArrayList<ArrayList<Double>> bwList = new ArrayList<ArrayList<Double>>();
		if (_ht==null){
			return null; 
		}
		else{
			Set<String> set = _ht.keySet();
			for (String ee: set){
				ClientInterface client = UserInterfaces.userInterfaces.get(ee);
				ArrayList  bw = client.get_bw();
				
				// print 
				int count = 0;
				System.out.println( "********************************GWT COMPO\n\n");
				for ( Object e: bw ){
					System.out.println("bw:"+bw.get(count));
					count++;
				}
				System.out.println( "********************************GWT COMPO\n\n");
				
				bwList.add(bw);
				System.out.println( "GWT COMPO jitter añadido");
			}
			if(bwList==null){
				System.out.println( "GWT COMPO lista vacia ");
				return null;
			}
			else {
                /*
                 * Cut and resize the arrayList. Display only the last 30 elements
                 */
                resizeTo30(bwList);

				System.out.println("COMPO retorna la lista de jitter");
				return bwList;
			}
			
		}
		
	}
	
	public ArrayList<ArrayList<Double>> getLost(){
		ArrayList<ArrayList<Double>> lostList = new ArrayList<ArrayList<Double>>();
		if (_ht==null){
			return null; 
		}
		else{
			Set<String> set = _ht.keySet();
			for (String ee: set){
				ClientInterface client = UserInterfaces.userInterfaces.get(ee);
				ArrayList  lost = client.get_pl();
				
				// print 
				int count = 0;
				System.out.println( "********************************GWT COMPO\n\n");
				for ( Object e: lost ){
					System.out.println("lost:"+lost.get(count));
					count++;
				}
				System.out.println( "********************************GWT COMPO\n\n");
				
				lostList.add(lost);
				System.out.println( "GWT COMPO lost añadido");
			}
			if(lostList==null){
				System.out.println( "GWT COMPO lista vacia ");
				return null;
			}
			else {
                /*
                 * Cut and resize the arrayList. Display only the last 30 elements
                 */
                resizeTo30(lostList);

				System.out.println("COMPO retorna la lista de lost");
				return lostList;
			}
			
		}
		
	}
	
	
	public ArrayList<String> getInterface(){
		ArrayList<String> interList = new ArrayList<String>();
		if (_ht==null){
			return null; 
		}
		else{
			Set<String> set = _ht.keySet();
			for (String ee: set){
				ClientInterface client = UserInterfaces.userInterfaces.get(ee);
				interList.add(client.get_id());
				System.out.println( "interface añadida:"+client.get_id());
				
				
			}
			if(interList==null){
				return null; 
			}
			else{
				return interList;
			}
			
		}
	}
	

    private void resizeTo30(ArrayList recortar){

        if (recortar.size()>30) {
                int number_of_exclude_elements = (recortar.size() - 30);
                for(int j=0;j<number_of_exclude_elements; j++){
                recortar.remove(0);

        }
        }

}

}
